package com.medata.utils;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.medata.dataObject.Globalsetting;
import com.medata.exeptions.YunExceptions;
import com.medata.service.GlobalsettingService;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import javax.xml.namespace.QName;
import java.util.List;


/**
 * @author ：zb
 * @date ：Created in 2019/5/29 0029 19:02
 * @description：
 */

public class SolrCrudUtil {
    //基于原接口的查改删-->新版本没问题
    public static JsonObject simp_search(GlobalsettingService globalsettingService,String str) {
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("searchWebService"), new Object[]{str}, new Class[]{String.class});
            resultstr = (String) objects[0];
            XmlUtil.getUnreplace(resultstr);
            //20190910zb修复字符的问题
//            resultstr=XmlUtil.getUnreplace(resultstr);
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "检索单库的时候失败");
            }
            return returns;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "检索单库的时候出现bug");
        }
    }


    //20190830手动连接solr实现查改删除
    //查询所有数量
    public static Long dbtotal(String solrPathDb){
        HttpSolrServer solrServer=new HttpSolrServer(solrPathDb);
        SolrQuery solrQuery=new SolrQuery();
        solrQuery.set("q","*");
        try {
            QueryResponse response = solrServer.query(solrQuery,SolrRequest.METHOD.POST);
            long numFound = response.getResults().getNumFound();
            return numFound;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //按年份归类销售凭证
    public static Integer yearSale(String year,String solrPathDb){

        return null;
    }
    //获取过期合同


    //过期合同管理工具类
    public static void htmanager(SolrDocumentList results,List<String> list){
        if (results != null && !results.isEmpty()) {
            for (SolrDocument result:results){
                StringBuffer sb=new StringBuffer("");
                String docid = (String) result.get("id");
                Object duetime = result.get("duetime");
                Object duedate = result.get("duedate");
                Object docTitle = result.get("DocTitle");
                if (duetime!=null){
                    Integer compare = TimeUtil.compare((String) duetime);
                    if (compare<0){
                        sb.append(docid+"@@@"+duetime+"@@@"+docTitle);
                        list.add(sb.toString());
                    }
                }else if (duedate!=null){
                    Integer compare = TimeUtil.compare((String) duedate);
                    if (compare<0){
                        sb.append(docid+"@@@"+duedate+"@@@"+docTitle);
                        list.add(sb.toString());
                    }
                }
            }
        }
    }


    //portSearch-->新版本没问题
    public static JsonObject portSearch(GlobalsettingService globalsettingService,String str) {
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
//        String endPoint = "http://192.168.1.54:8080/TSK_SOLR/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("searchWebService"), new Object[]{str}, new Class[]{String.class});
            resultstr = (String) objects[0];
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "检索单库的时候失败");
            }
            return returns;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "检索单库的时候出现bug");
        }
    }



    public static void main(String[] args) {

    }
}
